<template>
  <div>
    <a-button type="primary" :loading="uploading" @click="handleUpload">
      一键上传
    </a-button>
  </div>
</template>
<script>
import moment from 'moment'
import { uploadFileFunction } from '@/api/public'
export default {
  props: ['info'],
  data() {
    return {
      uploading: false,
      id: 0,
      fileList:[]
    }
  },
  filters: {},
  created() {},
  mounted() {},
  computed: {},
  methods: {
    moment,
    //确认上传文件
    handleUpload() {
      this.$message.success('文件正在上传请稍等!') 
      this.fileList=[]
      let info = []
      this.info.forEach((item, index) => {
        if(item.fileList.length>0){
         this.fileList = [...this.fileList,item.fileList]  
         info.push(item)
        }
      })  
      let fun = this.traverse(info)   
      fun(0).then(console)  
    },
    //分批上传文件
    traverse(arr) {
      let i = 0
      let len = arr.length
      let that =this
      return function recursion() {
        if (i >= len) return Promise.resolve()
        let data = arr[i++]
        return new Promise((resolve, reject) => {
          // promise替换成你的异步请求
          const fileList = data.fileList        
          const formData = new FormData()
          fileList.forEach((file) => {
            formData.append('file', file)
          })
          that.$emit("btnloading",data.id)        
          setTimeout(() => {
            uploadFileFunction(formData, data.url).then((response) => {
              if (response.code == 200) {
                let result = '上传成功'
                resolve([result,data.id])
              } else {
                let result = `文件,上传失败:${response.msg}`
                resolve([result,data.id])
              }
            }, 1000)
          })
        }).then((d) => {         
          that.$emit("resultChange",d[0],d[1])
          recursion() // 把请求后的数据传给下一次调用
        })
      }
    },
   
  },
}
</script>






